from flask import request,jsonify,Blueprint
from db import db1
from comm import get_data

bp_course=Blueprint('course',__name__)
@bp_course.route('/zhiw',methods=['GET','POST','DELETE','PUT'])
def zhiw():
    if request.method=='POST':
        data=get_data()
        sql="insert into zhiwugl(name,bianm,paix) values('%s','%s',%d)"%(data['name'],data['bianm'],int(data['paix']))
        res=db1.insert(sql)
        return jsonify({'mes':"添加成功"})
    
    elif request.method=='GET':
        id=request.args.get('id')
        if id:
            sql="select * from zhiwugl where id =%d"%(int(id))
            res=db1.find_one(sql)
            return jsonify({'list':res})
        else:
            sql="select * from zhiwugl"
            res=db1.find(sql)
            return jsonify({'list':res})

    elif request.method=='DELETE':
        id=request.args.get('id')
        sql="delete from zhiwugl where id=%d"%(int(id))
        res=db1.insert(sql) 
        return jsonify({'mes':"删除成功"})
    else:
        data=get_data()
        print(data)
        sql="update zhiwugl set name='%s',bianm='%s',paix=%d where id=%d"%(data['name'],data['bianm'],int(data['paix']),int(data['id']))
        res=db1.insert(sql)
        return jsonify({'mes':"修改成功"})






